 import * as shipAPI from "@/API/ship";
 
 class PlayerManager { 
    public static getPlayer(id: string) { 
        world.querySelectorAll("player").forEach((player) => {
            if (player.player.userId === id) { return player; }
        });

        return null;
    }

    public static async getData(id: string) { 
        let player = this.getPlayer(id);
        return player ? new PlayerData(player)  : null;
    }

    public static async init(player: GamePlayerEntity, ship: shipAPI.shipCreatData) { 
        let temp = new shipAPI.PlayerShip(ship, player);
    }
}
interface IPlayerData { 
    money: number;
    exp: number;
    level: number;
    ships: [];
}

class PlayerData {
    db!: GameDataStorage<any>
    constructor(player: GamePlayerEntity){
        let temp = storage.getGroupStorage(player.player.userId);
        temp ? this.db = temp : player.player.kick()
    }

    async set(key: keyof IPlayerData, value: IPlayerData[typeof key]){
        await this.db.set(key, value)
        return
    }

    async get(key: keyof IPlayerData){
        let data = await this.db.get(key)
        return data
    }

    async add(key: keyof IPlayerData, v: number){
        if(typeof key != "string"){
            console.warn("禁止更改非数字类型")
        }

        await this.db.increment(key, v)
        return
    }
}

export { PlayerManager };


